package com.itboye.mengwu.app.ui.mymeng;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.MainApplication;
import com.itboye.mengwu.data.model.BabyBean;
import com.itboye.mengwu.data.presenter.BabyPresenter;
import com.itboye.mengwu.data.presenter.GetYanzhengMaPresenter;

public class ActivityChooseBaby extends BaseActivity implements Observer {
	ImageView back;
	TextView txt_title;
	TextView name;
	ListView choose_baby_list;
	TextView moren;
	ArrayList<BabyBean> baby = new ArrayList<BabyBean>();
	String uid;
	BabyPresenter presenter;
	TextView add_baby;
	RelativeLayout nodata;
	TextView nodata_tv;

	// ArrayList<BabyBean> arrayList;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_choose_baby;
	}

	public ActivityChooseBaby() {
		// TODO Auto-generated constructor stub
	}

	public ActivityChooseBaby(String uid) {
		// TODO Auto-generated constructor stub
		this.uid = uid;
	}

	@Override
	public void onMyCreate() {
		showProgressDialog("正在加载宝宝信息", true);
		// TODO Auto-generated method stub
		txt_title.setText("选择宝宝");
		OnClickListener();
		presenter = new BabyPresenter(this);
		presenter.BabyAll(SPUtils.get(this, null, SpPublicParams.UID, "") + "");

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		presenter.BabyAll(SPUtils.get(this, null, SpPublicParams.UID, "") + "");
	}

	class MyAdapter extends BaseAdapter {
		Context context;

		public MyAdapter(Context context) {
			// TODO Auto-generated constructor stub
			this.context = context;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return baby.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			if (convertView == null) {
				LayoutInflater inflater = LayoutInflater.from(context);
				convertView = inflater.inflate(R.layout.item_choose_baby, null);
				name = (TextView) convertView.findViewById(R.id.name);
				moren = (TextView) convertView.findViewById(R.id.moren);
				String babyName = baby.get(position).getBaby_name();
				name.setText(babyName);
				if ("1".equals(baby.get(position).getIs_default())) {
					moren.setVisibility(View.VISIBLE);
				} else {
					moren.setVisibility(View.GONE);
				}
			}
			return convertView;
		}
	}

	private void OnClickListener() {

		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		add_baby.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(ActivityChooseBaby.this,
						ActivityBabyAdd.class);
				startActivity(intent);

			}
		});
		choose_baby_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Log.d("hu", "click");
				Intent intent = new Intent(ActivityChooseBaby.this,
						ActivityBabyEdit.class);
				intent.putExtra("model", baby.get(arg2));
				startActivity(intent);
			}
		});
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity model = super.handlerError(data);
		if (model != null) {
			baby.clear();
			baby.addAll((ArrayList<BabyBean>) model.getData());
			if (baby.size() != 0) {
				nodata.setVisibility(View.GONE);
				choose_baby_list.setVisibility(View.VISIBLE);
				MyAdapter adapter = new MyAdapter(this);
				choose_baby_list.setAdapter(adapter);
				adapter.notifyDataSetChanged();
			} else {
				nodata.setVisibility(View.VISIBLE);
				nodata_tv.setText("还没有宝宝哦 ...");
				choose_baby_list.setVisibility(View.GONE);
			}
			// finish();

		} else {
			try {
				closeProgressDialog();
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		try {
			closeProgressDialog();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}